package com.usian.controller;

import com.usian.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("/user")
@RestController
public class UserControllrt {


    //去找
    @Autowired
    private LoadBalancerClient loadBalancerClient;


    //找到发送的
    @Autowired
    private RestTemplate restTemplate; //原生的服务调用


    @RequestMapping("/findUserById")
    public User findUserById(Integer userId){
        //找服务
        ServiceInstance choose = loadBalancerClient.choose("nacos-provider");
        //拼接请求地址
        String url = "http://"+choose.getHost()+":"+choose.getPort()+"/user/findUserByUserId/"+userId;
        //发起请求
        return restTemplate.getForObject(url,User.class);
    }

}
